package cn.gp.controller;

import cn.gp.common.PageInfo;
import cn.gp.common.R;
import cn.gp.entity.Flower;
import cn.gp.service.FlowerService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName FlowerController
 * @Description TODO
 * @Author He_Guoping
 * @Date 2023/5/28 17:26
 * @Version 1.0
 */
@Log4j
@RestController
@RequestMapping("/flower")
public class FlowerController {

    @Autowired
    private FlowerService flowerService;

    @GetMapping("/page")
    public R<PageInfo<Flower>> page(
            Integer page,
            Integer pageSize,
            @RequestParam(name = "name", defaultValue = "") String name,
            @RequestParam(name = "status", defaultValue = "1") Integer status,
            @RequestParam(name = "categOne") List<Integer> categOne,
            @RequestParam(name = "lowPrice", defaultValue = "0") BigDecimal lowPrice,
            @RequestParam(name = "upperPrice", defaultValue = "65535") BigDecimal upperPrice) {
        log.info("page: " + page + ", " + pageSize);
        log.info("price: " + lowPrice + ", " + upperPrice);
        return R.success(flowerService.page(page, pageSize, name, status, categOne, lowPrice, upperPrice));
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody Flower flower) {
        log.info("add_flower: " + flower);
        return flowerService.save(flower) ? R.success("success") : R.error("error");
     }

     @PutMapping("/update")
     public R<String> update(@RequestBody Flower flower) {
        log.info("flower_update: " + flower);
        return flowerService.update(flower) ? R.success("success") : R.error("error");
     }

     @DeleteMapping("/del/{id}")
     public R<String> deleteById(@PathVariable Integer id) {
        log.info("flower_del: " + id);
        return flowerService.deleteById(id) ? R.success("success") : R.error("error");
     }
}
